﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tunnel.Tools
{
    public static class FanTimeControl
    {
        public static DateTime LastFanTime { get; set; }

        public async static void Inti()
        {
            DBHelper dh = new DBHelper();
            DataTable dt = await dh.GetTableAsync("SELECT LastFanTime From M_LastFanTime");
            foreach(DataRow dr in dt.Rows)
            {
                LastFanTime = Convert.ToDateTime(dr["LastFanTime"].ToString());
            }
        }

        public static void SaveLastTime()
        {
            DBHelper dh = new DBHelper();
            dh.RunCommand("UPDATE M_LastFanTime SET LastFanTime=@lft", new System.Data.SqlClient.SqlParameter("@lft", LastFanTime.ToString()));
        }

        public static int getSeconds()
        {
            TimeSpan ts = new TimeSpan();
            DateTime dt1 = DateTime.Now;
            ts = dt1 - LastFanTime;

            double getSeconds = ts.TotalSeconds;

            int seconds = Properties.Settings.Default.fanIntervalTime - (int)getSeconds;
            if(seconds<=0)
            {
                return 0;
            }
            else
            {
                return seconds;
            }
        }
    }
}
